package edu.berkeley.cs160.innovationers.shoppersidekick.database;

import edu.berkeley.cs160.innovationers.shoppersidekick.Constants;
import edu.berkeley.cs160.innovationers.shoppersidekick.ShoppingList;
import edu.berkeley.cs160.innovationers.shoppersidekick.Item;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ListIterator;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class MyDB {
	private SQLiteDatabase db;
	private final Context context;
	private final MyDBhelper dbhelper;
	
	public MyDB (Context c){
	context = c;
	dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
	}
	public void close(){
		db.close();
	}
	public void open() throws SQLiteException{
		try {
			Log.v("MyDBhelper onCreate","test");

			db = dbhelper.getWritableDatabase();
			
		}catch(SQLiteException ex){
			Log.v("Open database exception caught", ex.getMessage());
			db = dbhelper.getReadableDatabase();
		}
	}
	public long insertdiary(String title, String content, String listName){
		try{
			ContentValues newTaskValue = new ContentValues();
			newTaskValue.put(Constants.ITEM_NAME, title);
			newTaskValue.put(Constants.QUANTITY_NAME, content);
			newTaskValue.put(Constants.LIST_NAME,listName);
			return db.insert(Constants.TABLE_NAME, null, newTaskValue);
		}catch(SQLiteException ex){
			Log.v("Insert into database exception caught", ex.getMessage());
			return -1;
		}
	}
	
	public int insertALLData(ArrayList<ShoppingList> sl){
		try{
			int slNum=0;
			int itemNum=0;
			for(slNum=0; slNum < sl.size();slNum++){
				for(itemNum=0;itemNum < sl.get(slNum).getItemList().size();itemNum++){
					String title, content, listName;
					ArrayList<Item> x = sl.get(slNum).getItemList();
					title = x.get(itemNum).getName();
					content = Integer.toString(x.get(itemNum).getQty());
					listName = sl.get(slNum).getName();
					
					ContentValues newTaskValue = new ContentValues();
					newTaskValue.put(Constants.ITEM_NAME, title);
					newTaskValue.put(Constants.QUANTITY_NAME, content);
					newTaskValue.put(Constants.LIST_NAME,listName);
					
					db.insert(Constants.TABLE_NAME, null, newTaskValue);
				}
			}
			
			return 1;
		}catch(SQLiteException ex){
			Log.v("Insert into database exception caught", ex.getMessage());
			return -1;
		}
	}
	public Cursor getdiaries(){
		Cursor c = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
		return c;
	}
	public Cursor getMembers(){
		Cursor c = db.query(Constants.TABLE_NAME, null, "quantity=?", new String[]{"999"}, null, null, null);
	return c;
	
	}
	public void deleteALLData(){
		try{
			String nullString = null;
			db.delete(Constants.TABLE_NAME, null, new String[]{nullString});
			return;
			}catch(SQLiteException ex){
				Log.v("Delete tablename in database exception caught", ex.getMessage());
				return;
			}
	}
	
	public int deleteList(String listName){
		try{
		return db.delete(Constants.TABLE_NAME, "listName=?", new String[]{listName});
		}catch(SQLiteException ex){
			Log.v("Delete tablename in database exception caught", ex.getMessage());
			return -1;
		}
	}
	public int deleteMember(String member){
		try{
		return db.delete(Constants.TABLE_NAME, "name=?", new String[]{member});
		}catch(SQLiteException ex){
			Log.v("Delete tablename in database exception caught", ex.getMessage());
			return -1;
		}
	}
	public int deleteMemberFromList(String member, String listName){
		try{
		return db.delete(Constants.TABLE_NAME, "name=? AND listName=?", new String[]{member, listName});
		}catch(SQLiteException ex){
			Log.v("Delete tablename in database exception caught", ex.getMessage());
			return -1;
		}
	}
	
	public int deletediary(String title, String listName){
	try{
		return db.delete(Constants.TABLE_NAME, "name=? AND listName=?", new String[]{title,listName});
	}catch(SQLiteException ex){
		Log.v("Delete item in database exception caught", ex.getMessage());
		return -1;
	}
	}
	
	public void renameList(String listName, String newListName){
		/*String updateListName = "UPDATE "+ Constants.TABLE_NAME+" SET "+Constants.LIST_NAME+" = '"+newListName+"' WHERE "+Constants.LIST_NAME+" = '"+listName+"'";
		Log.v("test1", "3");
		db.execSQL(updateListName);
		Log.v("test1", "4");*/
		
		ContentValues cv = new ContentValues();             
		cv.put(Constants.LIST_NAME, newListName);
		Log.v("test1", "4");
		db.update(Constants.TABLE_NAME, cv, Constants.LIST_NAME + "= ? ", new String[] { listName});
		Log.v("test1", "5");
	}
	public Cursor getMembersFromList(String listName){
		
		return db.query(Constants.TABLE_NAME, null, "listName=? AND quantity=?", new String[]{listName, "999"}, null, null, null);
	}
	
	
	public Cursor getspecificlist(String listName){
		
		return db.query(Constants.TABLE_NAME, null, "listName=?", new String[]{listName}, null, null, null);
	}
	
	public Cursor checkEmpty(){
		Cursor c;
		Log.v("test0", "0");
		if(db.getPageSize()!=1024)
		{
			Log.v("testX", Integer.toString((int) db.getPageSize()));
		c = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
		}else
			return null;
		return c;
		
		
	}
	
	
	
	//return a list of shopping list with items populated already
	/*
	 * Log.v("test0", "0");
        Cursor c = localDatabase.getdiaries();
        Log.v("test1", "1");
        
        startManagingCursor(c);
        
        	Log.v("test3", "3");
        	if(c.moveToFirst()){
        		Log.v("test4","4");
        		do{
        			Log.v("test5","5");
        			String itemName, quantity, listName;
        			itemName = c.getString(c.getColumnIndex(Constants.ITEM_NAME));
        			quantity = c.getString(c.getColumnIndex(Constants.QUANTITY_NAME));
        			listName = c.getString(c.getColumnIndex(Constants.LIST_NAME));
        			
        			Log.v("test6","6");
        			boolean checkList = false;
        			ListIterator ptr;
        			ptr = myList.listIterator();
        			Log.v("test7","7");
        			while(ptr.hasNext()){
        				if(ptr.next() == listName)
        					{
        					checkList = true;
        					break;
        					}
        			}
        			Log.v("test8","8");
        			if(checkList){
        				int index = myList.indexOf(listName);
            			myList.get(index).addItem(itemName, quantity);
        				
        			}else{
        				myList.add(new ShoppingList(listName));
        				int index = myList.indexOf(listName);
        				myList.get(index).addItem(itemName, quantity);
        				subList.add(listName);
        			}
        			Log.v("test9","9");
        			
        			
        		}while(c.moveToNext());
        	}
	 */
	
	
}